<?php

namespace App\Admin\Controllers;

use App\Admin\Actions\OrderReturnAddress\OrderReturnAddressDeleteAction;
use App\Admin\Grid\NGrid;
use App\Library\Logic\OperateLogLogic;
use App\Models\ShopUserAddressModel;
use App\Models\ShopUserModel;
use Encore\Admin\Facades\Admin;
use App\Library\Traits\AdminControllerTrait;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid\Displayers\Actions;

class ReturnAddressController extends AdminController
{
    use AdminControllerTrait;

    protected $title = '退货地址';

    protected function grid(): NGrid
    {
        $user = ShopUserModel::query()->where(['user_id' => Admin::user()->user_id])->first();
        $grid = new NGrid(new ShopUserAddressModel);
        $grid->model()->where('type', 1)->orderByDesc('created_at');

        if ($user->user_id !== 1) {
            if ($user->main_id === 0 || $user->power) $grid->model()->leftJoin('shop_user', 'shop_user.user_id', 'shop_user_address.main_id')->where('shop_user_address.main_id', $user->main_id);
            else $grid->model()->leftJoin('shop_user', 'shop_user.user_id', 'shop_user_address.user_Id')->where('shop_user_address.user_id', $user->user_id);
        }
        $grid->column('ua_id', 'ID')->sortable();
        $grid->column('ua_contacts', __('shop_user_address.uaContacts'));
        $grid->column('ua_contacts_mobile', __('shop_user_address.uaContactsMobile'));
        $grid->column('ua_code', __('shop_user_address.uaCode'));
        $grid->column('ua_province', __('shop_user_address.uaProvince'));
        $grid->column('ua_city', __('shop_user_address.uaCity'));
        $grid->column('ua_county', __('shop_user_address.uaCounty'));
        $grid->column('ua_address', __('shop_user_address.uaAddress'));
        $grid->column('ua_address_full', __('shop_user_address.uaAddressFull'));
        $grid->disableCreateButton(false);
        $grid->actions(function (Actions $actions) {
            $actions->disableEdit(false);
            $actions->disableDelete();
            $actions->add(new OrderReturnAddressDeleteAction);
            $actions->disableView();
        });
        $grid->disableActions(false);
        return $grid;
    }

    public function form(): Form
    {
        $user = ShopUserModel::query()->where(['user_id' => Admin::user()->user_id])->first();
        $form = new Form(new ShopUserAddressModel);
        $form->hidden('serial_number');
        $form->hidden('type')->value(1);
        $form->hidden('ua_address_full');
        $form->hidden('user_id')->value($user->user_id);
        $form->hidden('main_id')->value($user->main_id);
        $form->text('ua_contacts', __('shop_user_address.uaContacts'))->rules('required|string:255');
        $form->text('ua_contacts_mobile', __('shop_user_address.uaContactsMobile'))->attribute('maxlength', 11)
            ->rules('required|regex:' . __('com.mobileReg'))->required();
        $form->text('ua_code', __('shop_user_address.uaCode'))->attribute('maxlength', 6);
        $form->text('ua_province', __('shop_user_address.uaProvince'));
        $form->text('ua_city', __('shop_user_address.uaCity'));
        $form->text('ua_county', __('shop_user_address.uaCounty'));
        $form->text('ua_address', __('shop_user_address.uaAddress'));
        $form->saving(function (Form $form) {
            $form->serial_number = serialNumber();
            $form->type = 1;
            $form->main_id = !is_null($form->main_id) ? $form->main_id : $form->user_id;
            $form->ua_address_full = $form->ua_province . $form->ua_city . $form->ua_county . $form->ua_address;
        });
        OperateLogLogic::add(Admin::user()->user_id);
        return $form->setWidth(10);
    }
}
